/** * @Description:底部添加购物车按钮 * @author Administrator */
<template>
	<view class="addShopcar">
		<button class="bottomButton" @click="showConfirmModal = true" :disabled="isDisabled">下单</button>

		<!-- 检测菜单完整弹窗 -->
		<u-modal v-model="showModal" z-index="1076">
			<view class="slot-content">
				<p>以下菜单没有选择</p>
				<p>
					<span v-for="(item, idx) of noCheckedContent" :key="idx">{{ item.content }}、</span>
				</p>
			</view>
		</u-modal>
		
		<!-- 确认下单弹窗 -->
		<u-modal content="确定下单吗？" v-model="showConfirmModal" show-cancel-button="true" z-index="1075" @confirm="toShopcar" ref="uModal" :async-close="true"></u-modal>

		<u-toast ref="uToast" />
	</view>
</template>

<script>
import * as api from '@/api/apiList/user/order.js';

export default {
	props: {
		stopTime: {
			//截单时间
			type: String,
			required: true
		},
		menuId: {
			//菜单id
			type: String
		},
		date: {
			//当前选中的日期
			type: String
		}
	},
	data() {
		return {
			showModal: false, //是否显示提示弹窗
			noCheckedContent: [], //没有勾选的内容
			isDisabled: false ,//是否禁用
			showConfirmModal:false//是否显示确认弹窗
		};
	},
	methods: {
		async API_generateOrder(ordercosts, ordercost, sendcost, type, mnid, mndate, items) {
			//下单接口
			let result = {
				data: {
					ordercosts, //订单总额（包含配送费）
					ordercost, //订单金额（不包含配送费）
					sendcost, //额定配送费
					type, //类型 1-午餐、2-晚餐
					mnid, //菜单id
					mndate, //菜单日期
					items //订单子项
				}
			};
			return await api.generateOrder(result);
		},
		async toShopcar() {
			//添加到购物车按钮点击事件
			let menuList = this.$parent.$refs.menuList._props.menuList; //获取所有菜单
			let checkedMenuList = this.$parent.$refs.menuList._data.checkedMenuList; //获取选中的菜单
			let exptxt = this.$u.deepClone(this.$parent.$refs.menuList._data.exptxt); //套餐
			let current = this.$parent._data.current + 1; //午餐或者晚餐
			let sendcost = this.$store.state.userModule.enterprise.sendcost; //配送费
			let menuId = this.menuId; //菜单id
			let mndate = this.date; //选中日期
			let other = checkedMenuList.other;
			// let otherPrice = Number(checkedMenuList.other.price)
			let otherPrice = 0;
			other.forEach(item => {
				otherPrice += Number(item.price);
			});
			let bool = this.signFilter(menuList, checkedMenuList); //过滤

			if (bool) {
				let ordercosts = exptxt.price + sendcost;
				exptxt.content = checkedMenuList;
				let res = await this.API_generateOrder(String(ordercosts), String(exptxt.price), String(sendcost), String(current), menuId, mndate, [exptxt]); //提交订单
				console.log(res);
				if (res.data.code === 1000) {
					this.$refs.uToast.show({
						title: res.data.msg,
						type: 'success'
					});
				} else if (res.data.code === 1001) {
					this.$refs.uToast.show({
						title: res.data.msg,
						type: 'error'
					});
				}
				uni.switchTab({//刷新页面
				    url: '/pages/home/home'
				});
				this.$parent.$refs.menuList.clearCheckedMenuList()//清空选中的菜单
				this.showConfirmModal = false
			}
		},
		getGenerateOrderParams(menu, exptxt, sendcost, chekcedMenu) {
			//获取下单参数

			let data = {
				ordercosts, //订单总额（包含配送费）
				ordercost: exptxt.price, //订单金额（不包含配送费）
				sendcost, //额定配送费
				type: menu.type, //类型 1-午餐、2-晚餐
				mnid: menu.id, //菜单id
				mndate, //菜单日期
				items //订单子项
			};
			return data;
		},
		signFilter(menuList, obj) {
			//签约处理方式(必须所有类型都选中，漏选需要提醒)
			let bool = false;
			let arr = [];
			for (let key in obj) {
				if (menuList[key].length > 0) {
					if (key !== 'other') {
						//非其他
						if (!obj[key].title) {
							arr.push({
								key
							});
						}
					} else {
						//其他
						if (!obj[key].length) {
							arr.push({
								key
							});
						}
					}
				}
			}
			arr.forEach(item => {
				if (item.key === 'main') {
					item.content = '主菜';
				} else if (item.key === 'side') {
					item.content = '副菜';
				} else if (item.key === 'vegetables') {
					item.content = '青菜';
				} else if (item.key === 'staple') {
					item.content = '小食';
				} else if (item.key === 'other') {
					item.content = '其他';
				}
			});
			this.noCheckedContent = arr;
			if (!arr.length) {
				return (bool = true);
			} else {
				this.showModal = true;
				this.showConfirmModal = false
				return (bool = false);
			}
			
		}
	}
};
</script>

<style lang="scss" scoped>
.addShopcar {
	z-index: 99;
	position: fixed;
	left: 0;
	bottom: 50px;
	width: 100%;

	.bottomButton {
		padding: 0;
		background: #f43a3d;
		border-radius: 50rpx;
		color: #ffffff;
		letter-spacing: 1rpx;
		font-size: 30rpx;
	}
	.slot-content {
		font-size: 32rpx;
		p {
			text-align: center;
			padding-top: 30rpx;
			padding-bottom: 50rpx;
			line-height: 30%;
		}
		span {
			color: #dd6161;
			text-align: center;
			line-height: 30%;
			display: inline-block;
		}
	}
}
</style>
